#include <iostream>
/*
 * @Author: 缄默
 * @Date: 2021-09-15 23:17:53
 * @LastEditors: 缄默
 * @LastEditTime: 2021-12-13 15:54:05
 */
//product
class Product {
    //虚函数和纯虚函数可以去现在同一类中 该类成为抽象基类 
    //含有纯虚函数的类成为抽象基类
public:
    //虚函数可以直接使用  
    //析构函数最好定义为虚函数 特别对于有继承关系的类；
    //析构函数可以定义为纯虚函数 这样其所在的类为抽象基类不能创建实例话对象
    //虚函数必须实现 不然编译器会报错
    virtual ~Product() {}

    //纯虚函数 必须在派生类实现后才能实例化
    //不能实例化 子类实现过后成为虚函数可以实例化对象
    virtual void show() = 0; 
};


//产品1
class concreteProduct1 : public Product {
public:
    void show() {
        std::cout << "concreteProduct1 Show..." << std::endl; 
    }
    concreteProduct1 () {
        std::cout << "concreteProduct1..." << std::endl;
    }
    ~concreteProduct1 () {}
};

//具体产品2
class concreteProduct2 : public Product {
public:
    void show() {
        std::cout << "concreteProduct2 Show..." << std::endl; 
    }
    concreteProduct2 () {
        std::cout << "concreteProduct2..." << std::endl;
    }
    ~concreteProduct2 () {}
};

//factory
class Factory {
protected:
     Factory(/* args */) { }
public:
   virtual Product *createProduct() = 0;
    virtual ~Factory() { }
};


//具体工厂1
class concreteFactory1 : public Factory {
public:
    concreteFactory1() {
        std::cout << "concreteFactory1..." <<std::endl;
    }
    Product *createProduct() {
        //具体工厂1生产产品2
        return new concreteProduct2();
    }
    ~concreteFactory1() {}
};

//具体工厂2
class concreteFactory2 : public Factory {
public:
    concreteFactory2() {
        std::cout << "concreteFactory2..." <<std::endl;
    }
    Product *createProduct() {
        //具体工厂2生产产品1
        return new concreteProduct1();
    }
    ~concreteFactory2() {}
};

int main() {
    Factory *factory1 = new concreteFactory1();
    Factory *factory2 = new concreteFactory2();

    Product *product1 = factory2->createProduct();
    Product *product2 = factory1->createProduct();

    product1->show();
    product2->show();

    delete factory1;
    delete factory2;

    delete product1;
    delete product2;

    return 0; 
}